package com.guomei.service.settings.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.guomei.bean.settings.domain.Setting;
import com.guomei.bean.settings.vo.PageFooterSetting;
import com.guomei.mapper.settings.SettingMapper;
import com.guomei.service.settings.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 配置服务实现类
 */
@Service
@Slf4j
@Transactional
public class SettingServiceImpl implements SettingService {
    
    @Resource
    private SettingMapper settingMapper;
    
    @Resource
    private ObjectMapper objectMapper;
    
    /**
     * 页脚配置的固定key
     */
    private static final String PAGE_FOOTER_SETTING_KEY = "page_footer_setting";
    
    @Override
    public PageFooterSetting getPageFooterSetting() {
        LambdaQueryWrapper<Setting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setting::getKey, PAGE_FOOTER_SETTING_KEY);
        
        Setting setting = settingMapper.selectOne(wrapper);
        if (setting == null || setting.getValue() == null) {
            log.info("页脚配置不存在，返回默认配置");
            return new PageFooterSetting();
        }
        
        try {
            PageFooterSetting pageFooterSetting = objectMapper.readValue(
                setting.getValue(), 
                PageFooterSetting.class
            );
            log.info("成功获取页脚配置");
            return pageFooterSetting;
        } catch (JsonProcessingException e) {
            log.error("反序列化页脚配置失败", e);
            return new PageFooterSetting();
        }
    }
    
    @Override
    public boolean saveOrUpdatePageFooterSetting(PageFooterSetting pageFooterSetting) {
        try {
            String jsonValue = objectMapper.writeValueAsString(pageFooterSetting);
            
            LambdaQueryWrapper<Setting> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Setting::getKey, PAGE_FOOTER_SETTING_KEY);
            
            Setting existingSetting = settingMapper.selectOne(wrapper);
            
            if (existingSetting == null) {
                // 创建新配置
                Setting newSetting = new Setting();
                newSetting.setKey(PAGE_FOOTER_SETTING_KEY);
                newSetting.setValue(jsonValue);
                newSetting.setDescription("页脚配置信息");
                newSetting.setCreatedTime(LocalDateTime.now());
                newSetting.setUpdatedTime(LocalDateTime.now());
                
                int result = settingMapper.insert(newSetting);
                log.info("创建页脚配置：{}", result > 0 ? "成功" : "失败");
                return result > 0;
            } else {
                // 更新现有配置
                existingSetting.setValue(jsonValue);
                existingSetting.setUpdatedTime(LocalDateTime.now());
                
                int result = settingMapper.updateById(existingSetting);
                log.info("更新页脚配置：{}", result > 0 ? "成功" : "失败");
                return result > 0;
            }
        } catch (JsonProcessingException e) {
            log.error("序列化页脚配置失败", e);
            return false;
        }
    }
    
    @Override
    public boolean deletePageFooterSetting() {
        LambdaQueryWrapper<Setting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setting::getKey, PAGE_FOOTER_SETTING_KEY);
        
        int result = settingMapper.delete(wrapper);
        log.info("删除页脚配置：{}", result > 0 ? "成功" : "失败");
        return result > 0;
    }
    
    @Override
    public boolean existsPageFooterSetting() {
        LambdaQueryWrapper<Setting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setting::getKey, PAGE_FOOTER_SETTING_KEY);
        
        Long count = settingMapper.selectCount(wrapper);
        return count != null && count > 0;
    }
}

